package com.ilibrary.web.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.ilibrary.base.MyAjaxAction;
import com.ilibrary.constants.MyConstants;
import com.ilibrary.service.BorrowService;
import com.ilibrary.utils.MyTools;
import com.ilibrary.web.vo.BorrowVO;
import com.ilibrary.web.vo.UserInfoVO;

public class AjaxDoRenewAction extends MyAjaxAction {

	private BorrowService borrowService;
	
	public BorrowService getBorrowService() {
		return borrowService;
	}
	public void setBorrowService(BorrowService borrowService) {
		this.borrowService = borrowService;
	}

	@Override
	protected JSONObject doExecute(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		JSONObject jo = new JSONObject();
		int borrowId = Integer.parseInt(request.getParameter("borrowId"));
		UserInfoVO loginUser = (UserInfoVO) session.getAttribute(MyConstants.ILIBRARY_lOGIN_USER);
		BorrowVO borrowVO = borrowService.findById(borrowId);
		if(loginUser.getUserId().equals(borrowVO.getUserId()) && borrowVO.getBorrowRenewNum() < MyConstants.BORROW_RENEW_NUM_MAX) {
			borrowVO.setBorrowRenewNum(borrowVO.getBorrowRenewNum()+1);
			borrowVO.setBorrowDeadline(MyTools.getNextXDayDate(borrowVO.getBorrowDeadline(), MyConstants.BOOK_LOAN_DAY_MAX));
			borrowVO.setUpdateDt(new Date());
			borrowVO.setUpdateUser(loginUser.getUserId());
			borrowService.update(borrowVO);
			jo.put("success", true);
		} else {
			jo.put("success", false);
		}
		return jo;
	}

}
